package com.gh.controller;


import com.gh.config.StrategyContext;
import com.gh.dto.CalDto;
import com.gh.dto.OrderDto;
import com.gh.pojo.Order;
import com.gh.resultVo.ResultVo;

import com.gh.strategy.GetSumStrategy;
import com.gh.strategy.ProductStrategy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author heyu
 * @date 2022:11:06 10:00
 * 控制层入口
 */
@RestController
@RequestMapping("/cal")
public class GetSumController {

    @Resource
    private StrategyContext strategyContext;

    @PostMapping("/fruits/sum")
    public ResultVo calPrice(@RequestBody OrderDto orderDto) {

        HashMap<String, BigDecimal>  customerMap = new HashMap();

        List<CalDto> orderBoss = orderDto.getOrder();

        for (CalDto calDto : orderBoss) {

            String isDiscount4Sum = calDto.getIsDiscount4Sum();
            GetSumStrategy getSumStrategy = strategyContext.getGetSumStrategy(isDiscount4Sum);

            List<Order> orders = calDto.getOrders();

            List<BigDecimal> prices = new ArrayList<>();
            if (!CollectionUtils.isEmpty(orders)) {
                for (Order order : orders) {
                    String isDiscount = order.getIsDiscount();
                    ProductStrategy productStrategy = strategyContext.getProductStrategy(isDiscount);
                    BigDecimal price = productStrategy.calProductPrice(order.getPrice(), order.getNum(),order.getDiscount());
                    prices.add(price);
                }
            }

            BigDecimal sum = getSumStrategy.getSum(prices);

            customerMap.put(calDto.getCustomer(),sum);

        }


        /*String isDiscount4Sum = calDto.getIsDiscount4Sum();
        GetSumStrategy getSumStrategy = strategyContext.getGetSumStrategy(isDiscount4Sum);

        List<Order> orders = calDto.getOrders();

        List<BigDecimal> prices = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orders)) {
            for (Order order : orders) {
                String isDiscount = order.getIsDiscount();
                ProductStrategy productStrategy = strategyContext.getProductStrategy(isDiscount);
                BigDecimal price = productStrategy.calProductPrice(order.getPrice(), order.getNum(),order.getDiscount());
                prices.add(price);
            }
        }

        BigDecimal sum = getSumStrategy.getSum(prices);*/


        System.out.println("customerMap = " + customerMap);

        return ResultVo.ok(customerMap);
    }

}
